package com.GameSpace.CoreAssets;


import java.util.ArrayList;

import com.GameSpace.SameGame.R;
import com.GameSpace.SameGame.GameAds;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.SimpleAdapter.ViewBinder;
import android.view.LayoutInflater;


/**
 * @author zuleyha
 * Class for Opening Screen
 */
public class CategoryAdapter extends BaseAdapter implements View.OnClickListener{

	private ArrayList<CategoryInfo> categories;
	private LayoutInflater mInflater;
	private Context mContext;
	private Resources mResource;
	private StateListDrawable mStateListDrawable;
	Context ctx = null;
	
	
	public CategoryAdapter(Context c)
	{
		mContext = c;
        mInflater = LayoutInflater.from(mContext);
        mResource  = mContext.getResources();
		ctx = c;
        SetCategories();
        
		
	}
	
	private void SetCategories()
	{
		categories = new ArrayList<CategoryInfo>(10);
		String desc="Miss Spell is an original word game that allows players to " +
				"test their spelling and sharpen their word skills against the most " +
				"commonly misspelled words.";
		categories.add(new CategoryInfo(mResource.getDrawable(R.drawable.game1),"Miss Spell",desc));
		
		desc="Flood It is a simple game which player tries " +
				"to fill the whole board with the same " +
				"color in a limited amount of moves.";
		categories.add(new CategoryInfo(mResource.getDrawable(R.drawable.game2),"Flood it",desc));
		
		desc="The goal of the game is to get the red block out of the board. " +
				"You should slide the other blocks back and forth to clear the path " +
				"and let the red block move out of the way.";
		categories.add(new CategoryInfo(mResource.getDrawable(R.drawable.game3),"Unblock Me",desc));
		
		desc="In the SameGame, you need to eliminate as much balls as possible by making chains " +
				"of same-colored balls. The bigger chain you eliminate, more points you will gain.";
		categories.add(new CategoryInfo(mResource.getDrawable(R.drawable.game4),"Same Game",desc));
		desc="This is a classic word scramble game. In this game you will be given a set" +
				" of scrambled letters, and you are required to rearrange" +
				" them into a meaningful word.";
		categories.add(new CategoryInfo(mResource.getDrawable(R.drawable.game5),"Scramble",desc));
		
		desc="Solo Test is a famous board game in which the" +
				" player tries to leave the number of pegs as few as possible in the " +
				"shortest possible time.";
		categories.add(new CategoryInfo(mResource.getDrawable(R.drawable.game6),"Solo Test",desc));
		
		desc="Number Guess is a game in which a random number is set, and the aim of the player " +
				"is to correctly guess that random number.";
		categories.add(new CategoryInfo(mResource.getDrawable(R.drawable.game7),"Number Guess",desc));
		
		desc="Color Fun is a game that uses colors to measure mental and visual controls of your brain.";
		categories.add(new CategoryInfo(mResource.getDrawable(R.drawable.game8),"Color Fun",desc));
		
		desc="Advanced Brain Trainer is a game which measures the perceptual and visual intelligence" +
				" of the players. The main purpose of the game is stimulating all aspects of your" +
				" mind.";
		categories.add(new CategoryInfo(mResource.getDrawable(R.drawable.game9),"Brain Trainer",desc));
		
		desc="Four in a Line is a two-player game in which the players drop their colored" +
				" discs from the top into a suspended grid and aim to connect four of one's" +
				" own discs of the same color next to any direction " +
				"to win the game.";
		categories.add(new CategoryInfo(mResource.getDrawable(R.drawable.game10),"Four in a Line",desc));
				
		
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return categories.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * @author Engin
	 * draws the canvas
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		mInflater =  LayoutInflater.from(mContext);;
	        View row=convertView;
			CategoryWrapper wrapper=null;
			if (row == null) 
			{
				row = mInflater.inflate(R.layout.game_ads_wrapper, parent, false);
				wrapper=new CategoryWrapper(row);
				row.setTag(wrapper);
			}
			wrapper=(CategoryWrapper)row.getTag();
			//final ResultBook book = getItem(position);
			Log.e("category",String.valueOf(categories.size()));
			wrapper.get_ctgName().setText(categories.get(position).ctgName);
			wrapper.get_ctgDef().setText(categories.get(position).ctgId);
			//wrapper.setId(String.valueOf(categories_id[position]));
			wrapper.getIcon().setImageDrawable(categories.get(position).ctgIcon);
			return(row);
		
			
	}
	/**
	 * @author halil
	 * called when user clicks on a category
	 * MarketList activity is called with category parameter
	 */
	/*
    public void onClick(View v) {
    	
    	
    	Log.e("do","sss");
    	if ( isNetworkAvailable(this.ctx)==false   ) 
        {
        	new AlertDialog.Builder(ctx)
			.setTitle("Bağlantı Hatasi !")
			.setMessage("Lütfen internet Baglantinizi Kontrol Ediniz")
			.setNeutralButton("Kapat", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dlg, int sumthin) {
			// do nothing – it will close on its own
			}
			})
			.show();                 
        } 
        else
        {
    	
        	String catName = (String) v.getTag();  //sana kategory id dondurecek
        	Intent intent=new Intent(this.mContext,MarketList.class);
        	intent.putExtra("cat", catName);
        	intent.putExtra("methodName", "methodApps");
        	this.mContext.startActivity(intent);
        }
        
    }
    
    */
    public static boolean isNetworkAvailable(Context context) {
    	
    	         ConnectivityManager connectivity = (ConnectivityManager) context
    	                 .getSystemService(Context.CONNECTIVITY_SERVICE);
    	        if (connectivity == null) {
    	             Log.w("tag", "couldn't get connectivity manager");
    	
    	       } else {
    	             NetworkInfo[] info = connectivity.getAllNetworkInfo();
    	
    	            if (info != null) {
    	                for (int i = 0; i < info.length; i++) {
    	
    	                     if (info[i].getState() == NetworkInfo.State.CONNECTED) {
    	
    	                         return true;
    	
    	                     }
    
    	                 }
    	
    	            }
    	         }
    	         return false;
    	     }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		Log.e("aaa","cccc");
		if ( isNetworkAvailable(this.ctx)==false   ) 
        {
        	new AlertDialog.Builder(ctx)
			.setTitle("Bağlantı Hatasi !")
			.setMessage("Lütfen internet Baglantinizi Kontrol Ediniz")
			.setNeutralButton("Kapat", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dlg, int sumthin) {
			// do nothing – it will close on its own
			}
			})
			.show();
        } 
        else
        {
    	
        	String catName = (String) v.getTag();  //sana kategory id dondurecek
        	Intent intent=new Intent(this.mContext,GameAds.class);
        	intent.putExtra("cat", catName);
        	intent.putExtra("methodName", "methodApps");
        	this.mContext.startActivity(intent);
        }
	}
    	

    
}
